package com.css.dzj.dzjap.modules.project.generecord.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.annotation.SysLog;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.project.generecord.entity.Generecord;
import com.css.dzj.dzjap.modules.project.generecord.service.GenerecordService;
import com.css.dzj.dzjap.modules.publick.entity.ApPublicEntity;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 一般工程备案
 *
 * @author liyuan
 */
@RestController
@RequestMapping("/generecord")
public class GenerecordController {
    @Autowired
    private GenerecordService generecordService;
    @Autowired
    BaseService baseService;
    /**
     * 多条件分页
     *
     * @param pageNo
     * @param pageSize
     * @param proName
     * @param unitName
     * @param checkStatus
     * @param reportStatus
     * @param reCreateDepartment
     * @param reCreateUser
     * @param proAddress         项目地址，开发写为非,必需性false,正式为true
     * @return
     */
    @GetMapping("/getPage")
    public R getPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
                     @RequestParam(name = "proName", required = false) String proName,
                     @RequestParam(name = "unitName", required = false) String unitName,
                     @RequestParam(name = "checkStatus", required = false) String checkStatus,
                     @RequestParam(name = "reportStatus", required = false) String reportStatus,
                     @RequestParam(name = "reCreateDepartment", required = false) String reCreateDepartment,
                     @RequestParam(name = "reCreateUser", required = false) String reCreateUser,
                     @RequestParam(name = "proAddress", required = false) String proAddress,
                     @RequestParam(name = "orgLevel", required = false) String orgLevel) {
        // 根据项目厂址判断权限，先注释掉
        if (StringUtils.isEmpty(proAddress))
            return R.error("您没有权限！");
        PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
        QueryResults<Generecord> page = generecordService.getPage(proName, unitName, reCreateUser, checkStatus,
                reportStatus, reCreateDepartment, proAddress, orgLevel, pageRequest);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data", page);
    }

    /**
     * 根据ID查看详情
     *
     * @param
     * @return
     */
    @GetMapping("/getInfo")
    public R getGenerecordById(@RequestParam("Id") String reId) {
        try {
            Generecord generecord = generecordService.queryById(reId);
            baseService.logSuccess(3,"查询成功","2");
            return R.ok().put("data", generecord);
        } catch (Exception e) {
            baseService.logFailed(3,"查询失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 提交(暂存并且提交的合体)
     *
     * @param generecord
     * @return
     */
    @SysLog("一般工程备案：提交(暂存并且提交的合体)")
    @PostMapping("/add")
    public R add(@RequestBody Generecord generecord) {
        try {
            // 1已提交，待审核
            generecord.setCheckStatus("1");
            generecordService.save(generecord);
            baseService.logSuccess(3,"添加成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"添加失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 暂存即保存
     *
     * @param generecord
     * @return
     */
    @SysLog("一般工程备案：暂存")
    @PostMapping("/zcadd")
    public R zcadd(@RequestBody Generecord generecord) {
        try {
            // 设置初始状态，0：未提交，暂存; 未上传
            generecord.setCheckStatus("0");
            generecordService.save(generecord);
            baseService.logSuccess(3,"添加成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"添加失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     *
     * @param generecord
     * @return
     */
    @SysLog("一般工程备案：修改后还是暂存状态")
    @PostMapping("/update")
    public R update(@RequestBody Generecord generecord) {
        try {
            // 若已经提交，修改后提交状态置为0：我提交
            if (!"0".equals(generecord.getCheckStatus())) {
                generecord.setCheckStatus("0");
            }
            Generecord targetGenerecord = generecordService.queryById(generecord.getReId());
            // 空的值不覆盖原有数据
            BeanUtil.copyBeanNotEmptyToBean(generecord, targetGenerecord);
            System.out.println(targetGenerecord.toString());
            generecordService.update(targetGenerecord);
            baseService.logSuccess(3,"修改成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"修改失败","2");
            return R.error(e.getMessage());
        }
    }

    @SysLog("一般工程备案：修改后是提交状态")
    @PostMapping("/updateSubmit")
    public R updateSubmit(@RequestBody Generecord generecord) {
        try {
            // 若已经提交，修改后提交状态置为0：我提交
            if ("0".equals(generecord.getCheckStatus())) {
                generecord.setCheckStatus("1");
            }
            Generecord targetGenerecord = generecordService.queryById(generecord.getReId());
            // 空的值不覆盖原有数据
            BeanUtil.copyBeanNotEmptyToBean(generecord, targetGenerecord);
            System.out.println(targetGenerecord.toString());
            generecordService.update(targetGenerecord);
            baseService.logSuccess(3,"修改成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"修改失败","2");
            return R.error(e.getMessage());
        }
    }

    @SysLog("一般工程备案：审核")
    @PostMapping("/check")
    public R check(@RequestBody Generecord generecord) {
        try {
            Generecord targetGenerecord = generecordService.queryById(generecord.getReId());
            // 空的值不覆盖原有数据
            BeanUtil.copyBeanNotEmptyToBean(generecord, targetGenerecord);
            generecordService.check(targetGenerecord);
            baseService.logSuccess(3,"修改成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"修改失败","2");
            return R.error(e.getMessage());
        }
    }

    @SysLog("一般工程备案：提交")
    @PostMapping("/submit/{id}")
    public R submit(@PathVariable String id) {
        try {
            // 更改上报状态
            generecordService.updateSubmitStatus(id);
            baseService.logSuccess(3,"保存成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"保存失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 上报
     *
     * @param id
     * @return
     */
    @SysLog("一般工程备案：上报")
    @PostMapping("/report/{id}")
    public R report(@PathVariable String id) {
        try {
            // 更改上报状态
            generecordService.updateReportStatus(id);
            baseService.logSuccess(3,"修改成功","2");
            return R.ok();
        } catch (Exception e) {
            baseService.logFailed(3,"修改失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @SysLog("一般工程备案：根据ID逻辑删除")
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        try {
            generecordService.logicDel(id);
            baseService.logSuccess(3,"删除成功","2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3,"删除成功","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 一般工程，中国地震局用户添加拟制公告
     * @param apPublicEntity
     * @return
     */
    @SysLog("一般工程备案：拟制公告")
    @PostMapping("/addPublick")
    public R addPublick(@RequestBody ApPublicEntity apPublicEntity) {
        try {
            generecordService.addPublick(apPublicEntity);
            baseService.logSuccess(3,"添加成功","2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3,"添加失败","2");
            return R.error(e.getMessage());
        }
    }

}
